package common.util;

import edu.princeton.cs.algs4.StdRandom;

/**
 * @author quanlinlin on 2018/10/26 9:37.
 * @version 1.0
 */
public class ArrayUtil {

    public static int[] getRandomArray() {
        int length = StdRandom.uniform(50);
        int[] array = new int[length];
        for (int i = 0; i < length; i++) {
            array[i] = StdRandom.uniform(100);
        }
        return array;
    }

    /**
     * 打印出的结果是：[1:xx,2:yy,3:zz]等
     *
     * @param a
     * @return
     */
    public static <T> String arrayToString(T[] a) {
        if (a == null)
            return "null";
        int iMax = a.length - 1;
        if (iMax == -1)
            return "[]";

        StringBuilder b = new StringBuilder();
        b.append('[');
        for (int i = 0; ; i++) {
            b.append(i).append(":").append(a[i]);
            if (i == iMax)
                return b.append(']').toString();
            b.append(", ");
        }

    }

}


